<?php


class HistoryService extends TP_Service {
    private $role;
    public function __construct() {
        parent::__construct();
        $this->load->model("SModel/Login_history_model");
        $this->role = $this->getRole($_SESSION[SESS_UUID]);
    }


    /**
     * 方法名 登录历史
     * 
     * @Name 登录历史
     * @Desc 方法描述
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      DJC
     * @Date 2022-02-19 16:32:11
     * @return      [type]                   [description]
     */

    public function historyList($param, $export = false) {
        if (!$export) {
            $param = array_filter($param, 'filtrfunction');
            if (!isset($param['pageNo'], $param['pageSize'])) {
                exit(retJson(ErrCode::ErrCode_10002));
            }

            $conarr['pageNo'] = $param['pageNo'];
            $conarr['pageSize'] = $param['pageSize'];
        }


        $conarr['where'] = "1=1";


        if (!empty($param['usercode'])) {
            $conarr['where'] .= sql_format(" AND usercode LIKE '%{0}%'", $param['usercode']);
        }
        if (!empty($param['name'])) {
            $conarr['where'] .= sql_format(" AND name LIKE '%{0}%'", $param['name']);
        }
        if (!empty($param['mobile'])) {
            $conarr['where'] .= sql_format(" AND mobile LIKE '{0}%'", $param['mobile']);
        }
        if (!empty($param['email'])) {
            $conarr['where'] .= sql_format(" AND email LIKE '{0}%'", $param['email']);
        }
        if (!empty($param['created']) && is_array($param['created'])) {
            $conarr['where'] .= sql_format(" AND created BETWEEN '{0}' AND '{1}'", $param['created'][0], $param['created'][1]);
        }



        if ($export) {
            $list = $this->Login_history_model->get_list_full($conarr);
        } else {
            $list = $this->Login_history_model->get_page_list($conarr);
        }


        return $list;
    }




    /**
     * 方法名 exportHistory
     * 
     * @Name exportHistory
     * @Desc 登录历史导出
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      DJC
     * @Date 2022-02-21 09:31:23
     * @return      [type]                   [description]
     */
    public function exportHistory($param) {
        $list = $this->historyList($param, true);

        $this->load->library('ProjectService/Xlsxwriter');
        $writer = new XLSXWriter();

        if (empty($list)) {
            exit(retJson(ErrCode::ErrCode_10902));
        }
        // 设置列类型和宽度
        $col_type = ['string', 'string', 'string', 'string', 'string', 'string', 'string', 'string'];
        $widths = ['suppress_row' => true, 'widths' => [10, 20, 20, 20, 30, 30, 35, 25]];
        $writer->writeSheetHeader('sheet1', $col_type, $widths);

        $title = ['ID', '账号', '姓名', '联系方式', '联系邮箱', '登录ip', 'ip所属地址', '登录时间'];

        $writer->writeSheetRow('sheet1', ['登录历史导出'], ['valign' => 'center', 'halign' => 'center', 'height' => 55, 'font' => '宋体', 'font-size' => 16, 'font-style' => 'bold']);
        $writer->markMergedCell('sheet1', $start_row = 0, $start_col = 0, $end_row = 0, $end_col = 8);
        $styles = ['font' => '宋体', 'font-size' => 10, 'halign' => 'center', 'border' => 'left,right,top,bottom', 'valign' => "center", 'border-style' => "thin", 'height' => 30, 'font-style' => 'bold'];
        $writer->writeSheetRow('sheet1', $title, $styles);

        foreach ($list as $k => $v) {
            $item = ['id' => $v['id'], 'usercode' => $v['usercode'], 'name' => $v['name'], 'mobile' => $v['mobile'], 'email' => $v['email'], 'ip' => $v['ip'], 'ip_address' => $v['ip_address'], 'created' => $v['created']];
            $writer->writeSheetRow('sheet1', $item, $styles);
        }



        $path = 'export/order/';
        if (!file_exists($path)) {
            mkdir($path, 0777);
        }
        $file = $path . '登陆历史' . time() . '.xlsx';
        $writer->writeToFile($file);
        return array('path' => $file);
    }
}
